package com.sso.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.sso.controller.LoginController;
import com.sso.util.ContextDefine;

public class LoginInterceptor extends HandlerInterceptorAdapter {
	protected final transient Log log = LogFactory.getLog(LoginInterceptor.class);
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		if(handler instanceof LoginController) {
			return true;
		} else {
			if(request.getSession() != null && request.getSession().getAttribute(ContextDefine.LOGIN_USER) != null) {
				return true;
			} else if(request.getSession() != null && request.getSession().getAttribute(ContextDefine.LOGIN_ADMIN) != null) {
				response.sendRedirect("/login.do?method=logout");
				return false; 
			}
			return true;
		}
	}
}
